package com.ehotting.edsta.circuitDesign.gen.element.helper;



public class NumberHelper {

    public static boolean isDigital(String str) {
        return str != null && str.matches("^[0-9]+$");
    }

    public static boolean isInteger(String s) {
        return s != null && s.matches("[-\\+]?[\\d]+$");
    }

    public static boolean isDouble(String s) {
        return s != null && s.matches("[-\\+]?[.\\d]*$");
    }

    public static Double parseDouble(String s) {
        return isDouble(s) ? Double.parseDouble(s) : 0.0D;
    }

    public static Float parseFloat(String s) {
        return isDouble(s) ? Float.parseFloat(s) : 0.0f;
    }

    public static Integer parseInteger(String s) {
        if (isInteger(s)) {
            return Integer.parseInt(s);
        } else {
            if (isDouble(s)) {
                s = s.substring(0, s.indexOf("."));
                if (isDigital(s)) {
                    return Integer.parseInt(s);
                }
            }

            return 0;
        }
    }

    public static Long parseLong(String s) {
        if (isInteger(s)) {
            return Long.parseLong(s);
        } else {
            if (isDouble(s)) {
                s = s.substring(0, s.indexOf("."));
                if (isDigital(s)) {
                    return Long.parseLong(s);
                }
            }
            return 0L;
        }
    }

    public static String convertFloat2(String s) {
        return s != null && isDouble(s) ? String.format("%.2f", parseDouble(s)) : null;
    }

    public static String convertFloat(String s) {
        return s != null && isDouble(s) ? String.format("%.0f", parseDouble(s)) : null;
    }

    public static Double convertFloat2(Double s) {
        return s == null ? null : parseDouble(String.format("%.2f", s));
    }


}

